package io.geekidea.springbootplus.system.controller;

import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.entity.SysCartItem;
import io.geekidea.springbootplus.system.entity.SysOrder;
import io.geekidea.springbootplus.system.service.SysOrderService;
import io.geekidea.springbootplus.system.vo.SysCartItemQueryVo;
import io.geekidea.springbootplus.system.vo.SysOrderQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Controller
@RequestMapping("/sysOrder")
@Module("system")
@Api(value = "订单API", tags = {"订单管理"})
public class SysOrderController extends BaseController {
    @Autowired
    private SysOrderService sysOrderService;

    @GetMapping("/orderList/{memberId}")
    @OperationLog(name="获取用户订单列表",type= OperationLogType.LIST)
    @ApiOperation(value="获取用户订单列表",response = SysOrderQueryVo.class)
    public ApiResult<List<SysOrderQueryVo>> getSysOrderListByMemberId(@PathVariable(value ="memberId")Integer memberId)throws Exception{
        List<SysOrderQueryVo> order= sysOrderService.selectAllOrderByMemberId(memberId);
        return ApiResult.ok(order);
    }


    @PostMapping( "/order/add")
    @OperationLog(name = "添加订单", type = OperationLogType.ADD)
    @ApiOperation(value ="添加订单",response = ApiResult. class)
    public ApiResult<Boolean> addOrder(@Validated(Add.class) @RequestBody SysOrder order) throws Exception {
        boolean flag = sysOrderService.addOrder(order);
        return ApiResult.result(flag);

    }

    @PostMapping("/order/delete/{id}")
    @OperationLog(name = "删除订单", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除订单", response = ApiResult.class)
    public ApiResult<Boolean> deleteOneOrder(@PathVariable("id")Long orderId) throws Exception {
        boolean flag = sysOrderService.deleteOneOrder(orderId);
        return ApiResult.result(flag);
    }
}
